<?php

namespace app\service;


class Http
{
    /**
     * get请求
     * @param $url
     * @param int $timeout
     * @param string $head
     * @return bool|string
     */
    public static function get($url, $timeout = 30, $head = '')
    {
        $head = ($head = '') ? FALSE : $head;
        $ch = curl_init();
        #设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        //Url
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        #设置header
        curl_setopt($ch, CURLOPT_HEADER, $head);
        #要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    /**
     * post请求
     * @param $url
     * @param $data
     * @param int $timeout
     * @param string $head
     * @return bool|string
     */
    public static function post($url, $data, $timeout = 30, $head = '')
    {
        $data = http_build_query($data);
        $head = ($head = '') ? FALSE : $head;
        $ch = curl_init();
        #设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        #Url
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        #设置header
        curl_setopt($ch, CURLOPT_HEADER, $head);
        #要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        #post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}